package com.cao.meet.swipelist;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.LinearLayout;

import com.cao.meet.swipelist.view.MyLinearLayout;

public class MoveActivity extends AppCompatActivity {

    private Button  btnScrollBy;
    private MyLinearLayout rootLinLayout;

    private LinearLayout itemRoot;
    private int mlastX = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move);

        initViews();

    }

    private void initViews() {
//        final LinearLayout roots = (LinearLayout) findViewById(R.id.roots);
//        Button btn_scroll_to = (Button) findViewById(R.id.btn_scroll_to);
//        btn_scroll_to.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                roots.scrollTo(100,100);
//            }
//        });
//        Button btn_scroll_by = (Button) findViewById(R.id.btn_scroll_by);
//        btn_scroll_by.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                roots.scrollBy(50,50);
//            }
//        });
//        Button btn_scroll_reset = (Button) findViewById(R.id.btn_scroll_reset);
//        btn_scroll_reset.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                roots.scrollTo(0,0);
//            }
//        });



        /*btnScrollBy = (Button) findViewById(R.id.btn_scroll_by);
        rootLinLayout = (MyLinearLayout) findViewById(R.id.roots);

        btnScrollBy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rootLinLayout.scrollBy(50, 50);
            }
        });*/




        itemRoot =    (LinearLayout)findViewById(R.id.lin_root);




    }


    /**
     * 监听手指滑动时间
     * 这个布局当你滑动一段距离后，会自动滑动到一段距离后停止
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int maxLength = dipToPx(this, MAX_WIDTH);

        int scrollX = itemRoot.getScrollX();
        int x = (int) event.getX();
        int newScrollX = scrollX + mlastX - x;

        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (newScrollX < 0) {
                newScrollX = 0;
            } else if (newScrollX > maxLength) {
                newScrollX = maxLength;
            }
            itemRoot.scrollTo(newScrollX, 0);
        }else if(event.getAction() == MotionEvent.ACTION_UP){
            if (scrollX > maxLength/2){
                newScrollX = maxLength;
            }else {
                newScrollX = 0;
            }
            itemRoot.scrollTo(newScrollX, 0);
        }
        mlastX = x;

        return super.onTouchEvent(event);   
    }


    private final int MAX_WIDTH = 200;

    /**
     * 利用dipToPx（）函数将DIP转换成PX
     * @param context
     * @param dip
     * @return
     */
    private int dipToPx(Context context, int dip) {
        return (int) (dip * context.getResources().getDisplayMetrics().density + 0.5f);
    }


}
